#include <QFile>
#include <QBuffer>
#include <QMessageBox>
#include <QTextCodec>
#include <QFileDialog>
#include <QDateTime>

#include "emaildialog.h"
#include "ui_emaildialog.h"
#include "pop3invoker.h"
#include "EmailMessage.h"
EmailDialog::EmailDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EmailDialog)
{
    ui->setupUi(this);
}

EmailDialog::~EmailDialog()
{
    delete ui;
}

void EmailDialog::on_receiveButton_clicked()
{
    POP3Invoker p;
    QFile f("F:\\ppsfile\\aa.eml");
    p.Connect("pop3.163.com");
    p.Login("iamcomingin@163.com","51yuyu.com");
    p.Retrieve(&f,1);
    p.Quit();
    /*
    ui->receiveButton->setCheckable(true);
    ui->receiveButton->setChecked(true);
    QString file = QFileDialog::getOpenFileName(this,
                                                tr("Open"),
                                                ".",
                        "E-mails (*.eml)");
    if(!file.isEmpty())
    {
        QFile f(file);
        EmailMessage mail;
        mail.open(&f);
        QStringList attachList = mail.attachments();

        for (int i = 0; i < attachList.size();++i)
        {
            QByteArray fileBuffer,contentType;
            mail.readAttachment(attachList.at(i),fileBuffer,contentType);
            QFile sd("F:\\IceForTest\\" + attachList.at(i));
            if(sd.open(QIODevice::WriteOnly))
            {
                sd.write(fileBuffer);
                sd.close();
            }
        }
        ui->fromEdit->setText(mail.getFrom());
        QStringList receiverList = mail.getTo();
        QString receiverListString;
        for(QStringList::const_iterator iter = receiverList.constBegin();iter != receiverList.constEnd();++iter)
        {
            receiverListString += *iter;
            receiverListString+= ' ';
        }
        ui->toEdit->setText(receiverListString);
        ui->dateEdit->setText(mail.getDate().toString());
        ui->subjectEdit->setText(mail.getSubject());
        ui->contentText->setText(mail.text());
    }
    */
}
